import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ToastController, AlertController, ViewController} from 'ionic-angular';
import {SportService} from "../../providers/SportService";
import {STATUS_SUCCESS} from "../../providers/Constants";
import {SportModel} from "../../model/SportModel";
import {BaseUI} from "../../common/baseui";

import {USERFACE_PATH, LOCAL_SPORT_TYPE_IMG_PATH, SPORT_BG_IMG_PATH} from "../../providers/Constants";

import {StorageService} from "../../providers/StorageService";
import {UserInfo} from "../../model/UserInfo";
import {Login} from "../login/login";
import {CheckUtil} from "../../common/CheckUtil";
import {JoinSportPersonPage} from "../join-sport-person/join-sport-person";

declare var AMap;

@IonicPage()
@Component({
  selector: 'page-sport-detail',
  templateUrl: 'sport-detail.html',
})
export class SportDetailPage extends BaseUI {

  errorMessage: string;
  userFacePath: string;
  localSportTypeImgPath: string;
  sportBgImgPath: string;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public sportService: SportService,
              public sportModel: SportModel,
              public toastCtrl: ToastController,
              public storageService: StorageService,
              public alertCtrl: AlertController,
              public checkUtil: CheckUtil,
              public viewCtrl: ViewController) {
    super();
    this.userFacePath = USERFACE_PATH;
    this.localSportTypeImgPath = LOCAL_SPORT_TYPE_IMG_PATH;
    this.sportBgImgPath = SPORT_BG_IMG_PATH;

  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad SportDetailPage');
    this.getSportDetail();
    // var map = new AMap.Map("container", {
    //   resizeEnable: true,
    //   center: [116.397428, 39.90923],//地图中心点
    //   zoom: 13 //地图显示的缩放级别
    // });
    // //步行导航
    // var walking = new AMap.Walking({
    //   map: map,
    //   panel: "panel"
    // });
    // walking.search([
    //   {keyword: '雅安市旅游汽车站',city:'雅安'},
    //   {keyword: '廊桥南(公交站)',city:'雅安'}
    // ]);
  }

  getSportDetail() {
    var id = this.navParams.get("id");
    var user = this.storageService.read<UserInfo>('UserInfo');
    var userId = 0;
    if (user != null) {
      userId = user.id;
    }
    this.sportService.getSportDetail(id, userId).subscribe(res => {
      if (res["status"] == STATUS_SUCCESS) {
        this.sportModel = res["data"];
      } else {
        super.showToast(this.toastCtrl, "获取活动信息失败", 'bottom');
      }
    }, error => {
      this.errorMessage = error;
      console.log(this.errorMessage);
      super.showToast(this.toastCtrl, "网络请求失败", 'middle');
    })
  }

  //参加运动
  joinSport() {
    if (this.checkUtil.checkUserLogin()) {
      if (this.storageService.read<UserInfo>('UserInfo') == null) {//用户未登录
        this.navCtrl.push(Login, {page: "SportDetailPage"});//跳转登录
      } else {
        this.sportService.joinSport(this.sportModel, this.storageService.read<UserInfo>('UserInfo').id).then(res => {
          super.showAlert(this.alertCtrl, null, res['msg'], '记住了');
          this.getSportDetail();
        });
      }
    } else {
      this.navCtrl.push(Login, {page: "SportDetailPage"});
    }

  }

  //取消参加运动
  cancelJoinSport() {
    if (this.checkUtil.checkUserLogin()) {
      var userId = this.storageService.read<UserInfo>('UserInfo').id;
      this.sportService.cancelJoinSport(this.sportModel.id, userId).subscribe(res => {
        // alert(res["msg"]);
        super.showAlert(this.alertCtrl, null, res['msg'], '确定');
        this.getSportDetail();
      });
    } else {
      this.navCtrl.push(Login, {page: "SportDetailPage"});
    }
  }

  //取消活动
  cancelSport() {
    if (this.checkUtil.checkUserLogin()) {
      let confirm = this.alertCtrl.create({
        message: '取消活动后，该活动将不会向其他用户展示。是否取消活动？',
        buttons: [
          {
            text: '否',
            handler: () => {
            }
          },
          {
            text: '是',
            handler: () => {
              this.sportService.cancelSport(this.sportModel.id).subscribe(res => {
                super.showToast(this.toastCtrl, res['msg'], 'bottom');
                if (res['status'] == 0) {
                  this.viewCtrl.dismiss();
                }
              });
            }
          }
        ]
      });
      confirm.present();
    } else {
      this.navCtrl.push(Login, {page: "SportDetailPage"});
    }
  }

  JoinSportPersonList(){
    this.navCtrl.push(JoinSportPersonPage,{sportId:this.sportModel.id});
  }

  //聊天
  opneChatDetail(userPhone){
    var user = this.storageService.read<UserInfo>('UserInfo');
    if(userPhone!=undefined && userPhone ==user.phone){
      super.showAlert(this.alertCtrl,null,'抱歉，不能向自己发起会话','确定');
      return;
    }
    this.navCtrl.push("ChatdetailPage",{chatUserNickName:this.sportModel.nickName,
      chatUserFace:this.sportModel.userFace,
      chatUserPhone:this.sportModel.userPhone,
      chatUserId:this.sportModel.userId});
  }

}
